local prefix = {}
local words = {}

function get(t, k)
    if k[1] == nil and k[2] == nil
    then
        return rawget(t, "")
    elseif k[1] == nil
    then
        return rawget(t, k[2])
    else
        return rawget(t, k[1].." "..k[2])
    end
end

function set(t, k, v)
    if k[1] == nil and k[2] == nil
    then
        rawset(t, "", v)
    elseif k[1] == nil
    then
        rawset(t, k[2], v)
    else
        rawset(t, k[1].." "..k[2], v)
    end
end

setmetatable(words, {__index=get, __newindex=set})

function add(w)
    if words[prefix] == nil
    then
        words[prefix] = {}
        table.insert(words[prefix], w)
    else
        local find = false
        for i,j in ipairs(words[prefix])
        do
            if j == w
            then
                find = true
                break
            end
        end
        if find == false
        then
            table.insert(words[prefix], w)
        end
    end

    prefix[1], prefix[2] = prefix[2], w
end

function gen()
    if words[prefix] == nil
    then
        return nil
    end
    
    math.randomseed(os.time())
    local i = math.random(#(words[prefix]))
    local w = words[prefix][i]
    prefix[1], prefix[2] = prefix[2], w 
    return w
end

local i = io.read("*all")
prefix = {nil, nil}
for w in string.gmatch(i, "%S+")
do
    add(w)
end

n = 1
max = 20
prefix = {nil, nil}
while true
do
    w = gen()
    if w == nil or n > max
    then
        break
    else
        io.write(w, " ")
    end
    n = n + 1
end
io.write("\n")

